<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { Router, RouterEvent, NavigationEnd } from '@angular/router';
import { Title } from '@angular/platform-browser';
import { Subject<% if (enableTranslation) { %>, of<% } %> } from 'rxjs';
<%_ if (enableTranslation) { _%>
import { TranslateModule, TranslateService, LangChangeEvent } from '@ngx-translate/core';
<%_ } _%>

import { MainComponent } from 'app/layouts/main/main.component';
import { TestModule } from '../../../test.module';
import { MockRouter } from '../../../helpers/mock-route.service';
<%_ if (enableI18nRTL) { _%>
import { FindLanguageFromKeyPipe } from 'app/shared/language/find-language-from-key.pipe';
<%_ } _%>

describe('Component Tests', () => {
  describe('MainComponent', () => {
    let comp: MainComponent;
    let fixture: ComponentFixture<MainComponent>;
    let router: MockRouter;
    const routerEventsSubject = new Subject<RouterEvent>();
    let titleService: Title;
    <%_ if (enableTranslation) { _%>
    let translateService: TranslateService;
    <%_ } _%>

    beforeEach(async(() => {
      TestBed.configureTestingModule({
        imports: [
          TestModule,
          <%_ if (enableTranslation) { _%>
          TranslateModule.forRoot(),
          <%_ } _%>
        ],
        declarations: [MainComponent],
        providers: [
          Title,
          <%_ if (enableI18nRTL) { _%>
          FindLanguageFromKeyPipe,
          <%_ } _%>
        ]
      })
        .overrideTemplate(MainComponent, '')
        .compileComponents();
    }));

    beforeEach(() => {
      fixture = TestBed.createComponent(MainComponent);
      comp = fixture.componentInstance;
      router = TestBed.get(Router);
      router.setEvents(routerEventsSubject.asObservable());
      titleService = TestBed.get(Title);
      <%_ if (enableTranslation) { _%>
      translateService = TestBed.get(TranslateService);
      <%_ } _%>
    });

    describe('page title', () => {
      let routerState: any;
      const defaultPageTitle = '<% if (enableTranslation) { %>global.title<% } else { %><%= capitalizedBaseName %><% } %>';
      const parentRoutePageTitle = 'parentTitle';
      const childRoutePageTitle = 'childTitle';
      const navigationEnd = new NavigationEnd(1, '', '');
      <%_ if (enableTranslation) { _%>
      const langChangeEvent: LangChangeEvent = { lang: '<%= nativeLanguage %>', translations: null };
      <%_ } _%>

      beforeEach(() => {
        routerState = { snapshot: { root: {} } };
        router.setRouterState(routerState);
        <%_ if (enableTranslation) { _%>
        spyOn(translateService, 'get').and.callFake((key: string) => {
          return of(key + ' translated');
        });
        translateService.currentLang = '<%= nativeLanguage %>';
        <%_ } _%>
        spyOn(titleService, 'setTitle');
        comp.ngOnInit();
      });

      describe('navigation end', () => {
        it('should set page title to default title if pageTitle is missing on routes', () => {
          // WHEN
          routerEventsSubject.next(navigationEnd);

          // THEN
          <%_ if (enableTranslation) { _%>
          expect(translateService.get).toHaveBeenCalledWith(defaultPageTitle);
          <%_ } _%>
          expect(titleService.setTitle).toHaveBeenCalledWith(defaultPageTitle<% if (enableTranslation) { %> + ' translated'<% } %>);
        });

        it('should set page title to root route pageTitle if there is no child routes', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };

          // WHEN
          routerEventsSubject.next(navigationEnd);

          // THEN
          <%_ if (enableTranslation) { _%>
          expect(translateService.get).toHaveBeenCalledWith(parentRoutePageTitle);
          <%_ } _%>
          expect(titleService.setTitle).toHaveBeenCalledWith(parentRoutePageTitle<% if (enableTranslation) { %> + ' translated'<% } %>);
        });

        it('should set page title to child route pageTitle if child routes exist and pageTitle is set for child route', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };
          routerState.snapshot.root.firstChild = { data: { pageTitle: childRoutePageTitle } };

          // WHEN
          routerEventsSubject.next(navigationEnd);

          // THEN
          <%_ if (enableTranslation) { _%>
          expect(translateService.get).toHaveBeenCalledWith(childRoutePageTitle);
          <%_ } _%>
          expect(titleService.setTitle).toHaveBeenCalledWith(childRoutePageTitle<% if (enableTranslation) { %> + ' translated'<% } %>);
        });

        it('should set page title to parent route pageTitle if child routes exists but pageTitle is not set for child route data', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };
          routerState.snapshot.root.firstChild = { data: {} };

          // WHEN
          routerEventsSubject.next(navigationEnd);

          // THEN
          <%_ if (enableTranslation) { _%>
          expect(translateService.get).toHaveBeenCalledWith(parentRoutePageTitle);
          <%_ } _%>
          expect(titleService.setTitle).toHaveBeenCalledWith(parentRoutePageTitle<% if (enableTranslation) { %> + ' translated'<% } %>);
        });

        it('should set page title to parent route pageTitle if child routes exists but data is not set for child route', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };
          routerState.snapshot.root.firstChild = {};

          // WHEN
          routerEventsSubject.next(navigationEnd);

          // THEN
          <%_ if (enableTranslation) { _%>
          expect(translateService.get).toHaveBeenCalledWith(parentRoutePageTitle);
          <%_ } _%>
          expect(titleService.setTitle).toHaveBeenCalledWith(parentRoutePageTitle<% if (enableTranslation) { %> + ' translated'<% } %>);
        });
      });

      <%_ if (enableTranslation) { _%>
      describe('language change', () => {
        it('should set page title to default title if pageTitle is missing on routes', () => {
          // WHEN
          translateService.onLangChange.emit(langChangeEvent);

          // THEN
          expect(translateService.get).toHaveBeenCalledWith(defaultPageTitle);
          expect(titleService.setTitle).toHaveBeenCalledWith(defaultPageTitle + ' translated');
        });

        it('should set page title to root route pageTitle if there is no child routes', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };

          // WHEN
          translateService.onLangChange.emit(langChangeEvent);

          // THEN
          expect(translateService.get).toHaveBeenCalledWith(parentRoutePageTitle);
          expect(titleService.setTitle).toHaveBeenCalledWith(parentRoutePageTitle + ' translated');
        });

        it('should set page title to child route pageTitle if child routes exist and pageTitle is set for child route', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };
          routerState.snapshot.root.firstChild = { data: { pageTitle: childRoutePageTitle } };

          // WHEN
          translateService.onLangChange.emit(langChangeEvent);

          // THEN
          expect(translateService.get).toHaveBeenCalledWith(childRoutePageTitle);
          expect(titleService.setTitle).toHaveBeenCalledWith(childRoutePageTitle + ' translated');
        });

        it('should set page title to parent route pageTitle if child routes exists but pageTitle is not set for child route data', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };
          routerState.snapshot.root.firstChild = { data: {} };

          // WHEN
          translateService.onLangChange.emit(langChangeEvent);

          // THEN
          expect(translateService.get).toHaveBeenCalledWith(parentRoutePageTitle);
          expect(titleService.setTitle).toHaveBeenCalledWith(parentRoutePageTitle + ' translated');
        });

        it('should set page title to parent route pageTitle if child routes exists but data is not set for child route', () => {
          // GIVEN
          routerState.snapshot.root.data = { pageTitle: parentRoutePageTitle };
          routerState.snapshot.root.firstChild = {};

          // WHEN
          translateService.onLangChange.emit(langChangeEvent);

          // THEN
          expect(translateService.get).toHaveBeenCalledWith(parentRoutePageTitle);
          expect(titleService.setTitle).toHaveBeenCalledWith(parentRoutePageTitle + ' translated');
        });
      });
      <%_ } _%>
    });
  });
});
